layui.use(['table', 'admin', 'ax', 'func', 'upload', 'laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var upload = layui.upload;
    var laydate = layui.laydate;

    //年月选择器
    laydate.render({
        elem: '#date'
        , type: 'month'
    });
    /**
     * 网上建店信息gcg_wsjd管理
     */
    var BtyMdFuzhumxz = {
        tableId: "btyMdFuzhumxzTable"
    };

    /**
     * 初始化表格的列
     */
    BtyMdFuzhumxz.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'btyMdFuzhumxzId', hide: true, title: '主键id'},
            {field: 'yaoma', width: 85, sort: true, title: '妖码'},
            {field: 'dm', minWidth: 250, sort: true, title: '门店'},
            {field: 'riqi', sort: true, title: '日期',width:110, templet: function (data) {
                    return layui.util.toDateString(data.riqi, "yyyy-MM-dd");
                }},
            {field: 'kemu', sort: true, title: '科目名称'},
            {field: 'bumendangan', sort: true, title: '部门档案名称'},
            {field: 'pingzhenghao', sort: true, title: '凭证号',width:100},
            {field: 'zaiyao', sort: true, title: '摘要'},
            {field: 'duifangKemu', sort: true, title: '对方科目'},
            {field: 'jie', sort: true, title: '借方',width:110},
            {field: 'dai', sort: true, title: '贷方',width:110},
            {field: 'fangxiang', width: 80, sort: true, title: '方向'},
            {field: 'yue', sort: true, title: '余额'},
            // {field: 'createTime', sort: true, title: '创建时间'},
            // {field: 'createUser', sort: true, title: 'create_user 创建人'},
            // {field: 'updateTime', sort: true, title: '修改时间'},
            // {field: 'updateUser', sort: true, title: 'update_user 修改人'},
            // {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BtyMdFuzhumxz.search = function () {
        var queryData = {};
        queryData['yaoma'] = $("#yaoma").val();
        queryData['date'] = $("#date").val();
        queryData['kemu'] = $("#kemu").val();

        table.reload(BtyMdFuzhumxz.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 跳转到添加页面
     */
    BtyMdFuzhumxz.jumpAddPage = function () {
        window.location.href = Feng.ctxPath + '/btyMdFuzhumxz/add'
    };

    /**
     * 跳转到编辑页面
     *
     * @param data 点击按钮时候的行数据
     */
    BtyMdFuzhumxz.jumpEditPage = function (data) {
        window.location.href = Feng.ctxPath + '/btyMdFuzhumxz/edit?btyMdFuzhumxzId=' + data.btyMdFuzhumxzId
    };

    /**
     * 导出excel按钮
     */
    BtyMdFuzhumxz.exportExcel = function () {
        var checkRows = table.checkStatus(BtyMdFuzhumxz.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BtyMdFuzhumxz.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyMdFuzhumxz/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BtyMdFuzhumxz.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("btyMdFuzhumxzId", data.btyMdFuzhumxzId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BtyMdFuzhumxz.tableId,
        url: Feng.ctxPath + '/btyMdFuzhumxz/list',
        page: true,
        limit:20,
        height: "full-59",
        cellMinWidth: 100,
        cols: BtyMdFuzhumxz.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BtyMdFuzhumxz.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

        BtyMdFuzhumxz.jumpAddPage();

    });

    // 导出excel
    $('#btnExp').click(function () {
        BtyMdFuzhumxz.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BtyMdFuzhumxz.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BtyMdFuzhumxz.jumpEditPage(data);
        } else if (layEvent === 'delete') {
            BtyMdFuzhumxz.onDeleteItem(data);
        }
    });


    /**
     * 导入费用明细表
     */
    var loadIndex;
    upload.render({
        elem: '#importMxz'
        , url: '/btyMdFuzhumxz/importFzmxz'
        , accept: "file"
        , async: false
        , exts: 'xls|xlsx|xlsm|xlt|xltx|xltm'
        , before: function () {
            loadIndex = layer.load(2, {shade: [0.15, '#ccc']});
            layer.msg("loading......正在导入数据，请稍等！");
        }
        , done: function (data) {
            if (data.code == 200) {
                layer.msg("导入数据成功！", {icon: 1, time: 1000}, function () {
                    layer.close(loadIndex);
                    window.location.reload();
                });
            } else {
                layer.close(loadIndex);
                layer.msg(data.message, {icon: 1, time: 2000, shade: 0.2});
            }

        },
    });

});
